package com.szq.middleware.db.router.config;

import com.szq.middleware.db.router.DBRouterConfig;
import com.szq.middleware.db.router.dynamic.DynamicDataSource;
import com.szq.middleware.db.router.util.PropertyUtil;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DataSourceAutoConfig implements EnvironmentAware {
    private Map<String,Map<String,Object>> dataSourceMap=new HashMap<>();

    private int dbCount; //分库数
    private int tbCount; //分表数

    @Bean
    public DBRouterConfig dbRouterConfig() {
        return new DBRouterConfig(dbCount, tbCount);
    }

    @Bean
    public DataSource dataSource(){
        //创建数据源
        Map<Object,Object> targetDataSources=new HashMap<>();
        for (String dbInfo:dataSourceMap.keySet()){
            Map<String,Object> objMap=dataSourceMap.get(dbInfo);
            targetDataSources.put(dbInfo,new DriverManagerDataSource(
                    objMap.get("url").toString(),objMap.get("username").toString(),
                    objMap.get("password").toString()
            ));
        }
        //设置数据源
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(targetDataSources);
        return dynamicDataSource;
    }

    @Override
    public void setEnvironment(Environment environment) {
        String prefix="router.jdbc.datasource.";

        dbCount=Integer.valueOf(environment.getProperty(prefix+"dbCount"));
        tbCount=Integer.valueOf(environment.getProperty(prefix+"tbCount"));

        String dataSource=environment.getProperty(prefix+"list");
        for (String dbInfo:dataSource.split(",")){
            Map<String,Object> dataSourceProps= PropertyUtil.handle(environment,prefix+dbInfo,Map.class);
            dataSourceMap.put(dbInfo,dataSourceProps);
        }

    }
}
